<?php
/**
 * ===============================================
 * 版权所有 2019-2050 青岛铁锤科技有限公司，并保留所有权利。
 * 网站地址: http://www.huaxianling.com
 * ===============================================
 * Author: Rag Hu
 * Created Time: 2019/10/19 18:00
 * Modified Time: 2019/10/19 18:00
 * Version: 2.0
 */

namespace App\Modules\Cashier\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetStoreBillCriteria extends BaseCriterias
{
    protected $store_id;
    protected $category;
    protected $status;
    protected $user_id;
	protected $is_barber;
    public function __construct($store_id,$category,$status,$user_id,$isBarber=false)
    {
        $this->store_id = $store_id;
        $this->category = $category;
        $this->status = $status;
        $this->user_id = $user_id;
        $this->is_barber = $isBarber;
    }

    public function apply($model, RepositoryInterface $repository)
    {
        $store_id = $this->store_id;
        $category = $this->category;
        $status = $this->status;
        $user_id = $this->user_id;
        $is_barber = $this->is_barber;
        return $model
            ->select([
                'id',
                'order_id',
                'user_id',
                'category',
                'amount',
                'receipts',
                'status',
                'main_recommend',
                'assist_recommend',
                'finish_time',
                'created_at'
            ])
	        ->when($is_barber,function($query)use($user_id){
	        	$query->where(['user_id'=>$user_id]);
	        })
            ->with(['user' => function ($query) {
                $query
                    ->select(['user.id', 'user.nick', 'picture.image as portrait'])
                    ->leftJoin('picture', 'picture.id', 'portrait');
            }])
            ->with(['main_recommend' => function ($query) {
                $query
                    ->select(['user.id', 'user.nick', 'picture.image as portrait'])
                    ->leftJoin('picture', 'picture.id', 'portrait');
            }])
            ->with(['assist_recommend' => function ($query) {
                $query
                    ->select(['user.id', 'user.nick', 'picture.image as portrait'])
                    ->leftJoin('picture', 'picture.id', 'portrait');
            }])
//	        ->leftJoin('order', function ($join){
//	        	$join->on('order.id','=','bill.order_id')->on('bill.category','=',1)->select(['id','order_no', 'assistant_id','member_card_id','payment'])
//                    ->leftJoin('order_item','order.id','order_item.order_id');
//	        })
//                $join->on('royalty.item_id', '=', 'order.id')->where(['royalty.type' => 1, 'royalty.user_id' => $map['order.barber_id'], 'royalty.store_id' => $map['order.store_id']]);
//	            $join->on('royalty.item_id','=','order.id')->where(['royalty.type'=>1]);
//            })
//            ->with(['order' => function ($query) {
//                $query->select(['id','order_no', 'assistant_id','member_card_id','payment'])
//                    ->with(['assistant' => function ($query) {
//                        $query
//                            ->select(['user.id', 'user.nick', 'picture.image as portrait'])
//                            ->leftJoin('picture', 'picture.id', 'portrait');
//                    }])
//                    ->with(['items']);
//            }])
//            ->with(['goodsOrder' => function ($query) {
//                $query->select(['id','payment'])
//	                ->with(['items'=>function($query){
//	                	$query->select(['id','goods_id'])
//			                ->with(['goods' => function ($query) {
//				                $query->select([
//					                'id',
//					                'name',
//					                'small_name',
//					                'image',
//					                'cat_id',
//					                'market_price'
//				                ])->with([
//					                'image' => function ($query) {
//						                $query->select('id', 'image');
//					                }
//				                ])->with([
//					                'goodsCategory' => function ($query) {
//						                $query->select('id', 'name', 'parent_id')
//							                ->with([
//								                'parent' => function ($query) {
//									                $query->select('id', 'name');
//								                }
//							                ]);
//					                }
//				                ]);
//			                }]);
//                    }]);
//            }])
//            ->with(['memberCardOrder' => function ($query) {
//                $query->select(['id','member_id','category','payment'])
//                    ->with(['memberCard'=>function($query){
//                        $query->select(['id','type_id','name','mobile','balance'])
//                            ->with(['cardType'=>function($query){
//                                $query->select('id','name');
//                            }]);
//                    }]);
//            }])
            ->when($category!==null,function ($query) use ($category){
                $query->where('category',$category);
            })
            ->when($status!==null,function ($query) use ($status){
                $query->where('status',$status);
            })
            ->where('store_id', $store_id)
            ->orderBy('id','DESC');
    }
}